package com.cn.yz.springboot;

import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import com.cn.yz.springboot.jersey.JerseyConfig;
import com.cn.yz.springboot.storage.StorageProperties;
import com.cn.yz.springboot.storage.StorageService;

@SpringBootApplication
@MapperScan("com.cn.yz.springboot.mapper")
@EnableConfigurationProperties(StorageProperties.class)
public class OriginalAppliaction  extends SpringBootServletInitializer {
	
	Logger logger = LoggerFactory.getLogger(OriginalAppliaction.class);
	
	@Override
	protected SpringApplicationBuilder configure(
			SpringApplicationBuilder application) {
		return application.sources(OriginalAppliaction.class);
	}
	
	@Bean
	public ServletRegistrationBean jerseyServlet() {
		ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/api/*");
		// our rest resources will be available in the path /api/*
		registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
		return registration;
	}
	
	public static void main(String[] args) {
		SpringApplication.run(OriginalAppliaction.class, args);
		
	}

	@Bean
	CommandLineRunner init(StorageService storageService) {
		return (args) -> {
			storageService.deleteAll();
			storageService.init();
		};
	}
}
